import axios from 'axios';

// APIJSON API服务
const apiService = {
  // 获取单个用户
  getUser: async (id) => {
    try {
      const response = await axios.post('/get', {
        User: {
          id: id
        }
      });
      return response.data;
    } catch (error) {
      console.error('获取用户失败:', error);
      throw error;
    }
  },
  
  // 获取用户列表
  getUserList: async (page = 0, count = 10) => {
    try {
      const response = await axios.post('/get', {
        '[]': {
          page: page,
          count: count,
          User: {}
        }
      });
      return response.data;
    } catch (error) {
      console.error('获取用户列表失败:', error);
      throw error;
    }
  },
  
  // 获取动态列表及其发布者
  getMomentList: async (page = 0, count = 10) => {
    try {
      const response = await axios.post('/get', {
        '[]': {
          page: page,
          count: count,
          Moment: {},
          User: {
            id: '@/Moment/userId'
          }
        }
      });
      return response.data;
    } catch (error) {
      console.error('获取动态列表失败:', error);
      throw error;
    }
  },
  
  // 获取动态及其评论
  getMomentWithComments: async (momentId) => {
    try {
      const response = await axios.post('/get', {
        Moment: {
          id: momentId
        },
        'Comment[]': {
          count: 3,
          Comment: {
            momentId: momentId
          }
        }
      });
      return response.data;
    } catch (error) {
      console.error('获取动态及评论失败:', error);
      throw error;
    }
  },
  
  // 自定义API请求
  customRequest: async (method, url, data) => {
    try {
      const response = await axios({
        method: method,
        url: url,
        data: data
      });
      return response.data;
    } catch (error) {
      console.error('自定义请求失败:', error);
      throw error;
    }
  }
};

export default apiService; 